from H3D import *
from H3DInterface import *
import random

def setPos():
  posx = (random.random() * worldWidth) - worldWidth/2;
  posy = (random.random() * worldHeight) - worldHeight/3;
  posz = (random.random() * worldDepth) - worldDepth/2;
  return Vec3f(posx,posy,posz);

random.seed();

numObjects = 12; # High numbers seem to cause issues
sphereSize = 0.005;
worldWidth = 0.4;
worldDepth = 0.4;
worldHeight = 0.4;
pos = Vec3f(0,0,0); # Default value
pos = setPos();

maxForce = 20;
force = random.random() * maxForce;
forceRadius = 0.05;
colour = RGB(force/maxForce,force/maxForce,0.2);

maxDim = 0.1;
boxSize = Vec3f(random.random()*maxDim,random.random()*maxDim,random.random()*maxDim);

group1, = references.getValue()

for i in range(0,numObjects):
  node, dn = createX3DNodeFromString( """
    <Transform DEF="A">
    <Shape>
    <Appearance><Material DEF="B"/></Appearance>
    <Sphere DEF="C"/>
    </Shape>
    </Transform>""" )

  group1.children.push_back(node)

  node, fn = createX3DNodeFromString( """
    <RepelForceEffect DEF="D" deviceIndex="0"/>""" )
  
  group1.children.push_back(node)

  node, cn = createX3DNodeFromString( """
    <Transform DEF="E">
    <Shape>
    <Appearance><Material/>
    <ImageTexture url="forcey.jpg" />
    <FrictionalSurface stiffness="1" staticFriction="0.4" dynamicFriction="0.2" /></Appearance>
    <Box DEF="F"/>
    </Shape>
    </Transform>""" )

  group1.children.push_back(node)
  
  dn['A'].translation.setValue(pos)
  dn['B'].diffuseColor.setValue( colour )
  dn['C'].radius.setValue(sphereSize)
  fn['D'].force.setValue(force)
  fn['D'].radius.setValue(forceRadius)

  #exPos = Vec3f(pos.x,pos.y + 0.15, pos.z);
  fn['D'].pos.setValue(pos)

  random.seed();
  pos = setPos();
  cn['E'].translation.setValue(pos)
  boxSize = Vec3f(random.random()*maxDim,random.random()*maxDim,random.random()*maxDim);
  cn['F'].size.setValue(boxSize)

  random.seed();
  pos = setPos();
  force = random.random() * maxForce;
  colour = RGB(force/maxForce,0.2,0.2);
